<?php

namespace app\admin\controller;

class Admin extends Base {

    //put your code here
    public function adminList() {
        $manager = db('manager')->join('t_role', 'mg_role_id=role_id')->field('t_manager.*,t_role.role_name')->select();
//        dump($manager);
        $this->assign('manager', $manager);
        return $this->fetch();
    }

    public function changeStatus0() {
        $id = input('param.id');
        if ($id) {
            $data = db('manager')->where('mg_id', $id)->update(['mg_status' => 0]);
            $this->redirect('adminList');
        }
    }

    public function changeStatus1() {
        $id = input('param.id');
        if ($id) {
            $data = db('manager')->where('mg_id', $id)->update(['mg_status' => 1]);
            $this->redirect('adminList');
        }
    }

    public function add() {
        $data = input('param.');
        if ($data) {
            $data['mg_time'] = date("Y-m-d H:i:s");
//           $data['mg_role_id'] = implode(',', $data['mg_role_id']);
//           dump($data);exit;
            $name = $data['mg_name'];

            $manager = db('manager')->where('mg_name', $name)->find();
            if (!$manager) {
                $info = db('manager')->insert($data);
                if ($info) {
                    echo '添加管理员成功!';
                } else {
                    $this->error('添加管理员失败!');
                }
            } else {
                $this->error('管理员已存在');
            }
        } else {
            $role = db('role')->select();
            $this->assign('role', $role);

            return $this->fetch();
        }
    }

    public function adminModify() {
        $id = input('param.id');
        if ($id) {
            $role = db('role')->select();
            $data = db('manager')->where('mg_id', $id)->find();
            $this->assign('role', $role);
            $this->assign('data', $data);
        }
        $arr = input('post.');
        if ($arr) {
            $name = $arr['mg_name'];
            $id = $arr['mg_id'];
            $info = db('manager')->where('mg_name', $name)->where("mg_id != $id")->find();
            if (!$info) {
                db('manager')->update($arr);
                echo '修改成功!';
                exit;
            } else {
//           $this->redirect('admin/admin/adminModify','管理员已存在');
                $this->error('管理员已存在');
            }
        }


        return $this->fetch();
    }

    public function role() {

        $data = db('role')->select();
        $this->assign('data', $data);


        return $this->fetch();
    }

    public function roleAdd() {

        $data = input('post.');
        if (!empty($data)) {
            $ids = $data['role_auth_ids'];
            $data['role_auth_ca'] = '';
            for ($i = 0; $i < count($ids); $i++) {
                $info = db('auth')->find($ids[$i]);
                if (($info['auth_pid']) !== 0) {
                    $data['role_auth_ca'] .= $info['auth_c'] . '-' . $info['auth_a'] . ',';
                }
            }
            $data['role_auth_ca'] = rtrim($data['role_auth_ca'], ',');
            $data['role_auth_ids'] = implode(',', $data['role_auth_ids']);

            $res = db('role')->insert($data);

            if ($res) {
                $this->success('角色添加成功');
            }
        }
        $auth1 = db('auth')->where('auth_pid', 0)->select();
        $auth2 = db('auth')->where('auth_pid != 0')->select();

        $this->assign('auth1', $auth1);
        $this->assign('auth2', $auth2);
        return $this->fetch();
    }

    //角色修改
    public function roleModify() {
        $roleid = input('param.roleid');
        $rlist = db('role')->find($roleid);
        $id = $rlist['role_auth_ids'];
        $id = explode(',', $id);

        $data = input('post.');
        if (!empty($data)) {
            $ids = $data['role_auth_ids'];
            $data['role_auth_ca'] = '';
            for ($i = 0; $i < count($ids); $i++) {
                $info = db('auth')->find($ids[$i]);
                if (($info['auth_pid']) !== 0) {
                    $data['role_auth_ca'] .= $info['auth_c'] . '-' . $info['auth_a'] . ',';
                }
            }
            $data['role_auth_ca'] = rtrim($data['role_auth_ca'], ',');
            $data['role_auth_ids'] = implode(',', $data['role_auth_ids']);

            $res = db('role')->update($data);

            if ($res) {
                $this->success('角色修改成功');
            }
        }
        $auth1 = db('auth')->where('auth_pid', 0)->select();
        $auth2 = db('auth')->where('auth_pid != 0')->select();

        $this->assign('id', $id);
        $this->assign('rlist', $rlist);
        $this->assign('auth1', $auth1);
        $this->assign('auth2', $auth2);
        return $this->fetch();
    }

    public function roledel(){
        $id = input('param.roleid');
        if ($id) {
            db('role')->delete($id);
        }
        $this->redirect('role');
    }
    
    public function permission() {
//         $count = db('auth')->count();

        $auth1 = db('auth')->where('auth_pid', 0)->select();

        $auth2 = db('auth')->where('auth_pid != 0')->select();
//       dump($auth1);
//       dump($auth2);
//       $this->assign('count',$count);
        $this->assign('auth1', $auth1);
        $this->assign('auth2', $auth2);
        return $this->fetch();
    }

    public function permissionadd() {
        $data = input('post.');
        if ($data) {
//            dump($data);exit;
            $info = db('auth')->insert($data);
            if ($info) {
                $this->success('权限添加成功!');
            }
            $this->error('权限添加失败!');
        }

        $auth = db('auth')->where('auth_pid', 0)->select();
        $this->assign('auth', $auth);

        return $this->fetch();
    }

    public function permissionModify() {
        $data = input('post.');
        if($data){
            $info = db('auth')->update($data);
            if($info){
                $this->success('权限修改成功!');
            }
        }
        $id = input('param.auth_id');
        $data = db('auth')->find($id);
        $this->assign('data', $data);

        $auth = db('auth')->where('auth_pid', 0)->select();
        $this->assign('auth', $auth);
        return $this->fetch();
    }

    public function permissiondel() {
        $id = input('param.authid');
        $info = db('auth')->delete($id);
        if ($info) {
            $this->redirect('permission');
        }
        $this->error('权限删除shibai!', 'permission');
    }

    public function del() {
        $id = input('param.id');
        if ($id) {
            db('manager')->delete($id);
        }
        $this->redirect('adminList');
    }

}
